
var floor= null;
var romm = null;
var id = null;
var check = null ;
var ff = null;
var name = null;
var phone = null;
var id_card1 = null;

var theData = new Vue({
    el: "#wrap",
    data: {
        nowPage: 1,   //当前页码
        totalPage: 0,  //总页码
        paginationItems: [   //页面数组
            /*{page:{}}*/  //数据格式
        ],
        tableItems: [       //表格数组
            /*{info:{}}*/  //数据格式
        ],
        flooritem:[

        ],
        roomitem:[

        ],
        setUp_user: {},     //配置的用户
        search_btn_remark: "",    //存放用户搜索内容
        token: token,        //input file 的加密
        time: unixtime       //input file 的加密
    },
    methods: {
        getList: function (nextPage) {   //获取每个分页的数据
            var that = this;
            var search = "";
            if (that.search_btn_remark != "") {
                search = that.search_btn_remark;
            }
            $.ajax({
                url: 'api/stay_out/get_stay_out_list.php',
                type: 'POST',
                data: {
                    token: token,
                    time: unixtime,
                    nextPage: nextPage,
                    search: search
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("请求超时");
                },
                success: function (res) {
                    $("#loading-mask").hide();
                    if (res.err == 0){
                        console.log(res);
                        that.tableItems = res.msg;
                        that.nowPage = nextPage;
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },
        getPagination: function () {    //获取分页数量
            var that = this;
            var search = "";
            if (that.search_btn_remark != "") {
                search = that.search_btn_remark;
            }
            $.ajax({
                url: 'api/stay_out/stay_out_pag_info.php',
                type: 'POST',
                data: {
                    token: token,
                    time: unixtime,
                    search: search
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("分页请求超时");
                },
                success: function (res) {
                    $("#loading-mask").hide();
                    if (res.err == 0) {
                        console.log(res.msg);
                        that.paginationItems = res.msg;
                        that.totalPage = res.total_page;
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },
        search: function () {      //查询
            var regSpace = /^\s+$/;
            if ($("#search-text").val() && !regSpace.test($("#search-text").val())) {
                this.search_btn_remark = $("#search-text").val();
                this.getPagination();
                this.getList(1);
            } else {
                MyAlert("请输入搜索内容");
            }
        },

        popAddStay:function(){//显示添加入住信息弹窗
            var that = this;
            check = true;
            $("#pop-add-stay").modal();
            this.findbelongroom();
        },

        shownameid:function () {//当输入完整的手机号时匹配人员信息中相应的姓名，身份证号码，判断是否已经入住
            var phonenumber = $("#pop-add-stay-phone").val();
            if(phonenumber==""){
                MyAlert("请输入手机号！");
            }
            else{
                $.ajax({
                    url: 'api/stay_out/find_name_idcard.php',
                    type: 'POST',
                    data: {
                        token: token,
                        time: unixtime,
                        phone:phonenumber,
                    },
                    dataType: 'JSON',
                    beforeSend: function () {
                        $("#loading-mask").show();
                    },
                    error: function () {
                        $("#loading-mask").hide();
                        MyAlert("分页请求超时");
                    },
                    success: function (res) {

                        $("#loading-mask").hide();
                        if (res.err == 0) {
                            if(res.msg!=""){
                                $("#pop-add-stay-name").val(res.msg[0].info.name);
                                $("#pop-add-stay-id_card").val(res.msg[0].info.id_card);
                            }
                        } else {
                            MyAlert(res.msg);
                            $("#pop-add-stay-phone").val("").focus();
                        }
                    }
                })
            }
        },

        getfloor:function(){//获取楼宇信息
            var that = this;
            $.ajax({
                url: 'api/room/get_floor.php',
                type: 'POST',
                data: {
                    token: token,
                    time: unixtime,
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("分页请求超时");
                },
                success: function (res) {
                    $("#loading-mask").hide();
                    if (res.err == 0) {
                        that.flooritem = res.msg;
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },

        findbelongroom:function(){//依照选择的楼宇显示所属该楼宇房间
            var that = this;
            var floor = null;
            if(check){
                floor = $("#pop-add-stay-floor option:selected").text();
            }
            else{
                floor = $("#pop-update-stay-floor option:selected").text();
            }
            var foor_id = null;
            if(floor!="" &&floor!=null){
                for(var i=0;i<that.flooritem.length;i++){//对比选择的楼宇名称获取楼宇ID
                    if(floor===that.flooritem[i].info.floor_name){
                        foor_id= that.flooritem[i].info.floor_id;
                    }
                }
                $.ajax({
                    url: 'api/stay_out/get_room.php',
                    type: 'POST',
                    data: {
                        token: token,
                        time: unixtime,
                        floor_id:foor_id,
                    },
                    dataType: 'JSON',
                    beforeSend: function () {
                        $("#loading-mask").show();
                    },
                    error: function () {
                        $("#loading-mask").hide();
                        MyAlert("分页请求超时");
                    },
                    success: function (res) {
                        $("#loading-mask").hide();
                        if (res.err == 0) {
                            console.log(res);
                            that.roomitem = res.msg;
                        } else {
                            MyAlert(res.msg);
                        }
                    }
                })

            }
        },

        popAddStaySubmit:function(){//入宿提交保存
            var that = this;
            var name = $("#pop-add-stay-name").val();
            var phone = $("#pop-add-stay-phone").val();
            var id_card = $("#pop-add-stay-id_card").val();
            var floor1 = $("#pop-add-stay-floor option:selected").text();
            var room = $("#pop-add-stay-room option:selected").text();
            var regPhone = /^1(3|4|5|6|7|8)\d{9}$/;
            var floor = null;
            if(name != "" && phone!=""&& floor1!="" && room!="") {
                if(regPhone.test(phone)){
                    MyAlert("请输入正确手机号码！");
                }
                for(var i=0;i<that.flooritem.length;i++){//对比选择的楼宇名称获取楼宇ID
                    if(floor1===that.flooritem[i].info.floor_name){
                        floor= that.flooritem[i].info.floor_id;
                    }
                }
                $.ajax({
                    url:'api/stay_out/add_stay_people.php',
                    type:'POST',
                    data:{
                        token: token,
                        time: unixtime,
                        name:name,
                        phone:phone,
                        id_card:id_card,
                        stay_floor_id:floor,
                        stay_floor_id1:floor1,
                        stay_room_id:room,
                    },
                    dataType: 'JSON',
                    beforeSend: function () {
                        $("#loading-mask").show();
                    },
                    error: function () {
                        $("#loading-mask").hide();
                    },
                    success:function (res) {
                        $("#loading-mask").hide();
                        if (res.err == 0) {
                            MyAlert(res.msg, function () {
                                window.location.reload();
                            });
                        } else {
                            MyAlert(res.msg);
                        }
                    }
                })
            }
            else{
                MyAlert("请完善入住信息！");
            }
        },

        popUpdataStay:function(item){//修改住宿信息弹窗
            var that = this;
            check = false;
            $("#pop-old-stay-floor").val(item.info.buildingnum);
            $("#pop-old-stay-room").val(item.info.roomnum);
            $("#pop-update-stay").modal();
            floor = item.info.buildingnum;//修改前楼宇名称
            romm = item.info.roomnum;//修改前房间名称
            id = item.info.id;
            ff = item.info.stay_room_name.floor_id;//修改前楼宇ID
            name = item.info.name;
            phone =item.info.phone;
            id_card1 = item.info.id_card;
            this.findbelongroom();
        },

        popUpdateStaySubmit:function(){//修改住宿信息提交
            var that=this;
            var floorid = $('#pop-update-stay-floor option:selected').text();//修改后楼宇名称
            var roomid = $('#pop-update-stay-room option:selected').text();//修改后房间名称
            var belongfloor=null;//修改后楼宇ID
            if(floorid!=""||roomid!=""){
                if(floor == floorid && romm==roomid){
                    MyAlert("信息未修改");
                }
                else{
                    for(var i=0;i<that.flooritem.length;i++){//对比选择的楼宇名称获取楼宇ID
                        if(floorid===that.flooritem[i].info.floor_name){
                            belongfloor= that.flooritem[i].info.floor_id;
                        }
                    }
                    console.log(id);
                    if(belongfloor==null)
                    {
                        belongfloor =floorid;
                    }
                    MyConfirm("确定修改?", function (){
                        $.ajax({
                            url: "api/stay_out/updataStayAuthority.php",
                            type: 'POST',
                            data: {
                                token: token,
                                time: unixtime,
                                floor_id:belongfloor,
                                room_name:roomid,
                                id:id,
                                floor_name:floorid,
                                floor_name1:floor,
                                floor_id1:ff,
                                room_name1:romm,
                                name:name,
                                phone:phone,
                                id_card:id_card1
                            },
                            dataType: 'JSON',
                            beforeSend: function () {
                                $("#loading-mask").show();
                            },
                            error: function () {
                                $("#loading-mask").hide();
                                alert("请求超时");
                            },
                            success: function (res) {
                                $("#loading-mask").hide();
                                if (res.err == 0) {
                                    MyAlert(res.msg, function () {
                                        console.log(123);
                                        window.location.reload();
                                    });
                                } else {
                                    MyAlert(res.msg);
                                }
                            }
                        })
                    })
                }
            }
            else{
                MyAlert("请填写必填信息");
            }
        },
        deleteStayAuthority:function(item){//退宿弹窗
            console.log(item);
            MyConfirm("确定退宿?", function () {
                $.ajax({
                    url: "api/stay_out/deleteStayInfo.php",
                    type: 'POST',
                    data: {
                        token: token,
                        time: unixtime,
                        index: item,
                    },
                    dataType: 'JSON',
                    beforeSend: function () {
                        $("#loading-mask").show();
                    },
                    error: function () {
                        $("#loading-mask").hide();
                        alert("请求超时");
                    },
                    success: function (res) {
                        $("#loading-mask").hide();
                        if (res.err == 0) {
                            MyAlert(res.msg, function () {
                                window.location.reload();
                            });
                        } else {
                            MyAlert(res.msg);
                        }
                    }
                })
            })
        },


    }
});
theData.getfloor();//首次加载获取楼宇信息
theData.getPagination();   //首次加载总页码
theData.getList(theData.nowPage);  //首次加载首个分页